package com.gandong8.derby.controller;

import com.gandong8.derby.core.annoation.NotLogin;
import com.gandong8.derby.core.constant.DOConstant;
import com.gandong8.derby.core.response.ResponseData;
import com.gandong8.derby.pojo.entry.manual.User;
import com.gandong8.derby.pojo.vo.request.UserReqVO;
import com.gandong8.derby.service.UserService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录
 * @author xiaoyutou
 */
@Controller
public class LoginController  extends BaseController{

    @Autowired
    UserService userService;

    @RequestMapping(value = DOConstant.LOGIN_PATH)
    @ResponseBody
    @NotLogin
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login.html");
        return modelAndView;
    }

    @RequestMapping(value = "/login/do",method = RequestMethod.POST)
    @NotLogin
    @ResponseBody
    public ResponseData loginDo(@Validated @RequestBody UserReqVO userReqVO) {
        userService.userLogin(userReqVO);
        return ResponseData.succ("/");
    }

    @RequestMapping(value = "/login/out")
    @NotLogin
    @ResponseBody
    public ResponseData loginOut() {
        userService.userLoginOut();
        return ResponseData.succ();
    }

    @RequestMapping(value = "/login/do2",method = RequestMethod.GET)
    @NotLogin
    @ResponseBody
    public ResponseData loginDo2(@Validated User user) {
        return ResponseData.succ();
    }

    @RequestMapping(value = "/login/do3",method = RequestMethod.POST)
    @NotLogin
    @ResponseBody
    public ResponseData loginDo3(@Validated User user) {
        return ResponseData.succ();
    }


    @NotLogin
    @RequestMapping(value = "/login/pic")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        try {
            CaptchaUtil.out(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
